package com.foreknow.example;

public class Order {
    private String orderId; //订单编号
    private double total;// 订单总额
    private OrderItem[] items;

    public Order(String orderId) {
        this.orderId = orderId;
        items = new OrderItem[3];
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public double getTotal() {
        calTotal();//调用计算总额的方法
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public OrderItem[] getItems() {
        return items;
    }

    public void setItems(OrderItem[] items) {
        this.items = items;
    }

    //将OrderItem对象添加到items数组中
    public void setItem(OrderItem item,int i) {
        this.items[i] = item;
    }

    //计算订单的总额
    public void calTotal() {
        double total = 0;
        for (int i = 0;i<items.length;i++) {
            if(items != null) {
                total += items[i].getPprice() * items[i].getNum();
            }
        }
        this.total = total;
    }
}
